package com.pnemec.util;

import java.awt.Component;
import java.awt.GridLayout;
import java.io.File;
import java.util.*;

import javax.swing.*;

public class Debug {
	private static Debug debug=null;
	
	private JFrame mainJF=null;
	private JTextArea logJTA=null;
	private int counter=0;
	
	
	private void show(Component parrent){
		if(mainJF==null){
			mainJF= new JFrame("Developer info window");
			if(parrent != null)
				mainJF.setLocationRelativeTo(parrent);
			JTabbedPane tabJTP = new JTabbedPane();
			
			
			//sysinfo
			JPanel sysInfoJP = new JPanel(new GridLayout(8,2));
			sysInfoJP.add(new JLabel("Operation System"));
			sysInfoJP.add(new JLabel(System.getProperty("os.name")));
					
			sysInfoJP.add(new JLabel("Architecture"));
			sysInfoJP.add(new JLabel(System.getProperty("os.arch")));
			
			sysInfoJP.add(new JLabel("Working path"));
			sysInfoJP.add(new JLabel(System.getProperty("user.dir")));
			
			sysInfoJP.add(new JLabel("User name"));
			sysInfoJP.add(new JLabel(System.getProperty("user.name")));
			
			sysInfoJP.add(new JLabel("Java version"));
			sysInfoJP.add(new JLabel(System.getProperty("java.version")));
			
			sysInfoJP.add(new JLabel("File separator (system)"));
			sysInfoJP.add(new JLabel(System.getProperty("file.separator")));
			
			sysInfoJP.add(new JLabel("File separator"));
			sysInfoJP.add(new JLabel(File.pathSeparator));
			
			sysInfoJP.add(new JLabel("Locale"));
			Locale l = Locale.getDefault();
			sysInfoJP.add(new JLabel(l.getCountry()));
			
			tabJTP.add(sysInfoJP,"Sys info");
			
			//logs
			JPanel logJP = new JPanel(new GridLayout(1,1));
			logJTA= new JTextArea();
			logJTA.setWrapStyleWord(false);
			logJTA.setLineWrap(false);
			logJTA.setEditable(false);
			JScrollPane sp = new JScrollPane(logJTA);
			logJP.add(sp);
			tabJTP.add(logJP,"Log");
			mainJF.getContentPane().add(tabJTP);
		}
		mainJF.pack();
		mainJF.setVisible(true);
	}
	public static void enableDebug(){
		Debug.debug = new Debug();
	}
	public static void showDebugWindow(){
		showDebugWindow(null);
	}
	public static void showDebugWindow(Component parrent){
		if(debug==null)return;
		debug.show(parrent);
	}
	public static void log(String log){
		if(debug==null)debug = new Debug();
		if(debug.logJTA==null){
			System.out.println("Debug facility: "+ log);
			return;
		}
		debug.counter++;
		debug.logJTA.append(String.valueOf(debug.counter)+"\t"+log+"\n");
		
		
	}
	
	
}
